
import pymysql

file_dir = r'e:/quxiao-source-code/paper/testData\test_data/'

table_args = [
#		['test10w', 'asset_meta_100000.out'],
#		['test50w', 'asset_meta_500000.out'],
#		['test100w', 'asset_meta_1000000.out'],
#		['test200w', 'asset_meta_2000000.out'],
		['test500w', 'asset_meta_5000000.out', 'asset_event_5000000.out']
		]

def add_meta(db_name, table_name, file_loc):
	conn = pymysql.connect(host='192.168.1.199', user='root', passwd='root', db=db_name)
	cur = conn.cursor()

	with open(file_loc, 'r') as in_file:
		print db_name
		for line in in_file:
			(asset_id, vendor, type, price, tag_id) = line.split('\t')
			sql = """
			SELECT * FROM %s WHERE id = %s
			""" % (table_name, asset_id)
			result_num = cur.execute(sql)
			if result_num != 0:
				print 'ignoring %s' % asset_id
				continue

			sql = '''
			INSERT INTO %s 
			SET 
				id		= %s,
				vendor	= "%s",
				type	= "%s",
				price	= %s,
				tag_id	= %s
			''' % (table_name, asset_id, vendor, type, price, tag_id)
			print asset_id
			cur.execute(sql)

	cur.close()
	conn.close()



def add_event (db_name, table_name, file_loc):
	conn = pymysql.connect(host='192.168.1.199', user='root', passwd='root', db=db_name)
	cur = conn.cursor()

	with open(file_loc, 'r') as in_file:
		print db_name, '.', table_name
		id = 0
		for line in in_file:
			(asset_id, level, event_info) = line.split('\t')
			id += 1

			sql = '''
			SELECT * FROM %s WHERE id = %s
			''' % (table_name, id)
			result_num = cur.execute(sql)
			if result_num != 0 :
				print 'ignoring %s' % id
				continue
			sql = '''
			INSERT INTO %s 
			SET 
				id			= %s,
				level		= "%s",
				info		= "%s",
				asset_id	= %s
			''' % (table_name, id, level, event_info, asset_id)
			print id
			cur.execute(sql)

	cur.close()
	conn.close()

if __name__ == '__main__':
	for arg in table_args:
		db_name = arg[0]
		event_file_loc = arg[2]
		add_event(db_name, 'Event', file_dir + event_file_loc)

